package com.example.play.repo

import com.blankj.utilcode.util.ToastUtils
import com.example.common.entity.CommendEntity
import com.example.common.entity.DanmuEntity
import com.example.common.entity.MovieVideoEntity
import com.example.common.entity.SimpleVideoEntity
import com.example.common.repo.BaseRepository
import com.example.http.OkHttpClientManager
import com.example.http.RetrofitManager
import com.example.play.api.PlayApiServer
import com.example.play.entity.AuthorEntity
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.RequestBody

class PlayVideoRepository:BaseRepository<PlayApiServer>() {
    override fun createApiServer(): PlayApiServer {
        return RetrofitManager.getRetrofit(OkHttpClientManager.createOkHttpClient()).create(PlayApiServer::class.java)
    }
    suspend fun getAuthorById(userid:String):Flow<AuthorEntity>{
        return flow {
            var result=apiServer.getAuthorById(userid)
            if (result.code==200){
                emit(result.data)
            }
        }
    }
    suspend fun getRecommendSimpleVideo(page: Int,pageSize: Int):Flow<List<SimpleVideoEntity>>{
        return flow {
            var result=apiServer.getRecommendSimpleVideo(page,pageSize)
            if (result.code==200){
                emit(result.data)
            }
        }
    }
    suspend fun getSimpleVideoByChannelId(channelId: String ,page: Int,pageSize: Int):Flow<List<SimpleVideoEntity>>{
        return flow {
            var result=apiServer.getSimpleVideoByChannelId(channelId,page,pageSize)
            if (result.code==200){
                emit(result.data)
            }
        }
    }
    suspend fun getRecommendMutilVideo(page: Int,pageSize: Int):Flow<List<MovieVideoEntity>>{
        return flow {
            var result=apiServer.getRecommendMutilVideo(page,pageSize)
            if (result.code==200){
                emit(result.data)
            }
        }
    }
    suspend fun getMutilVideoByTypeId(typeid: Int ,page: Int,pageSize: Int):Flow<List<MovieVideoEntity>>{
        return flow {
            var result=apiServer.getMutilVideoByTypeId(typeid,page,pageSize)
            if (result.code==200){
                emit(result.data)
            }
        }
    }
    suspend fun getVideoCommends(dataType: Int ,itemId: String):Flow<List<CommendEntity>>{
        return flow {
            var result=apiServer.getVideoCommends(dataType,itemId)
            if (result.code==200){
                emit(result.data)
            }
        }
    }
    suspend fun publishComment(requestBody: RequestBody):Flow<CommendEntity>{
        return flow {
            var result=apiServer.publishComment(requestBody)
            if(result.code==200){
                emit(result.data)
            }
        }
    }
    suspend fun publishDanmuMsg(requestBody: RequestBody):Flow<String>{
        return flow {
            var result=apiServer.publishDanmuMsg(requestBody)
            if(result.code==200){
                emit(result.data)
            }
        }
    }
    suspend fun getVideoDanmuMsg(datatype: Int ,itemid: String):Flow<List<DanmuEntity>>{
        return flow {
            var result=apiServer.getVideoDanmuMsg(datatype,itemid)
            if (result.code==200){
                emit(result.data)
            }
        }
    }
}